<?php
header ( "Content-type:text/html;charset=utf-8" );
class AdminAction extends Action{



       function _initialize(){

         if($_SESSION['ssauth']!='fuckworld')
        {

             $this->redirect('Index/admincp');
             exit;

         }

}



    //添加文章
    public function add()
    {



        $Model	=	new Model();
        $members_info=$Model->query("select * from ns_type order by path ");
foreach($members_info as $key=>$value)
    {

$dt="";


    if(substr_count($value['path'],',')>2)
   {

      for($i=0;$i<(substr_count($value['path'],',')-2);$i++)
        {
            $dt=$dt.'--';

        }

    }
    else
    {
     $dt="";

    }


          $members_info1[]=array

  (
      "id"=>$value["id"],
      "name"=>$dt."".$value["name"],
      "path"=>$value["path"]

  );



    		}

           // print_r($members_info1);exit;


        $this->assign('list',$members_info1);
        
        
        
        $dao=M('tag');
        $taglist=$dao->order("id desc")->findAll();
        
        
        $tgg="";
        foreach ($taglist as $value){
            
            $tgg.="<a class='addtag' mark='".$value['name']."' href='javascript:void(0)' >".$value['name']."</a>&nbsp;";
    
       }
        
        $this->assign("tgg",$tgg);
        $this->display('add');


    }

    // 首页
	public function index(){

        $Form	= M("article");


        import('ORG.Util.Pageutf');
        $listRows=20;
		$count=$Form->count();
		$p = new Pageutf($count,$listRows);


        // 按照id排序显示前6条记录
        $list=$Form->limit($p->firstRow.','.$listRows)->order('id desc')->findAll();
		//$list	=	$Form->order('id desc')->limit(100)->select();
		$this->assign('list',$list);
        //dump($p->show());
        $this->assign("page",$p->show());
		$this->display('index');
	}

	// 处理表单数据
	function insert() {
        $Article	=	M("article");
        if($_POST['title']==''){

            echo "标题不能为空";
            exit;

        }

        $rsgb=$Article->where(" title = '".$_POST['title']."' ")->find();
        if($rsgb){
         echo "标题重复";
         exit;
        }

        $upload=$_FILES["upfile"];

        if($upload[size]=='0')
       {


                        $data['content']=stripslashes($_POST['content']);
                        preg_match("/<img.+src=\"(.+)\"/i",$data['content'],$match);
                        //echo print_r($match1);exit;
                        if($match[1]!=''){
                        $cnt2=0;
                        while($cnt2<10 && ($imgcontent=@file_get_contents("http://".C("URL")."".$match[1]))===FALSE){ $cnt2++;}
                        $k=strrpos($match[1],".");
                        $ext=substr($match[1],$k+1);
                        $newfilename=rand(1,200)."".time().".".$ext;
                        @file_put_contents("./ns/Public/Uploads/".$newfilename,$imgcontent);
                        }


                       $data['title']=$_POST['title'];
                       if($match[1]!=''){
                       $data['pic']=$newfilename;
                       }
                       $data['tid']=$_POST['tid'];
                       $data['key']=$_POST['key'];
                       $data['desc']=$_POST['desc'];
                       $data['fan']=$_POST['fan'];
                       $data['creattime']=time();
                       $taglist = $_POST['taglist'];
                       $taglists = explode(",",$taglist);
                    if($result=$Article->data($data)->add()) {
                        
                        foreach($taglists as $value){
                            
                            
                            if($value != ''){
                       
                                  $dd=M("tag_article");
                                  
                                  $dc=M("tag");
                                  $one = $dc->where(" name = '".$value."' ")->find();
                                  
                                  $data1['a_id'] = $result;
                                  $data1['t_id'] = $one['id']; 
                                  
                                  $dd->add($data1);
                                  
                              }    
                        
                        
                       }
                        
                            

                            if($match[1]!=''){
                            import ( '@.ORG.CreatMiniature' );
                            $cm=new CreatMiniature();
                            $cm->SetVar("./ns/Public/Uploads/".$newfilename,"file");
                            $cm->BackFill("./ns/Public/Uploads/".$newfilename,122,88);

                            import ( '@.ORG.Gimage' );
                            preg_match_all("/<img.+src=\"(.+)\"/i",$data['content'],$match1);
                            $img = new Gimage();
                            foreach($match1[1] as $vl){

                                $ms=strrpos($vl,"/");
                                $jiehou=substr($vl,$ms+1);
                                $img->wm_image_name = "./ns/Public/Images/water_mark.gif";
                                $img->save_file="./ns/Public/Js/editor/attached/".$jiehou;
                                $img->create("./ns/Public/Js/editor/attached/".$jiehou);


                            }

                             }





                            //$this->assign('jumpUrl', '__APP__/Admin/index');
                            //$this->success('数据添加成功！');
                            echo "1";

                    }
                    else
                    {
                        header("Content-Type:text/html; charset=utf-8");
                        exit($Form->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');
                    }

        }
        else
        {

                       $filename=$upload[name];
                       $k=strrpos($filename,".");
                       $ext=substr($filename,$k+1);
                       //echo $ext;
                       $newfilename=rand(1,200)."".time().".".$ext;
                       move_uploaded_file($upload[tmp_name],"./ns/Public/Uploads/".$newfilename);


                       $Form	=	M("article");
                       $data['title']=$_POST['title'];
                       $data['content']=stripslashes($_POST['content']);
                       $data['tid']=$_POST['tid'];
                       $data['desc']=$_POST['desc'];
                       $data['key']=$_POST['key'];
                       $data['fan']=$_POST['fan'];
                       $data['creattime']=time();
                       $taglist = $_POST['taglist'];
                       $taglists = explode(",",$taglist);
                       $data['pic']=$newfilename;

                    if($result=$Form->data($data)->add())
                       {
                           
                           
                           foreach($taglists as $value){
                            
                            
                            if($value != ''){
                       
                                  $dd=M("tag_article");
                                  
                                  $dc=M("tag");
                                  $one = $dc->where(" name = '".$value."' ")->find();
                                  
                                  $data1['a_id'] = $result;
                                  $data1['t_id'] = $one['id']; 
                                  
                                  $dd->add($data1);
                                  
                              }
                              
                          }    

                        import ( '@.ORG.CreatMiniature' );
                        $cm=new CreatMiniature();
                        $cm->SetVar("./ns/Public/Uploads/".$newfilename,"file");
                        $cm->BackFill("./ns/Public/Uploads/".$newfilename,122,88);


                            import ( '@.ORG.Gimage' );
                            preg_match_all("/<img.+src=\"(.+)\"/i",$data['content'],$match1);
                            $img = new Gimage();
                            if(count($match1)!=0){
                            foreach($match1[1] as $vl){

                                $ms=strrpos($vl,"/");
                                $jiehou=substr($vl,$ms+1);
                                $img->wm_image_name = "./ns/Public/Images/water_mark.gif";
                                $img->save_file="./ns/Public/Js/editor/attached/".$jiehou;
                                $img->create("./ns/Public/Js/editor/attached/".$jiehou);

                            }
                            }

                        //$this->assign('jumpUrl', '__APP__/Admin/index');
                        //$this->success('数据添加成功！');
                        echo "1";



                       }

                    else
                        {
                        $this->error('数据写入错误');



                        }






        }



	}

    	// 删除页面
	public function delete() {
		if(!empty($_GET['id'])) {
			$Form	=	M("article");
			$result	=	$Form->delete($_GET['id']);
			if(false !== $result) {
				$this->assign('jumpUrl', '__APP__/Admin/index');
				$this->success('删除成功！');
			}else{
				$this->error('删除出错！');
			}
		}else{
			$this->error('删除项不存在！');
		}
	}
        	// 删除分类
	public function deletetype() {
		if(!empty($_GET['id'])) {
			$Form	=	M("type");
			$result	=	$Form->delete($_GET['id']);
			if(false !== $result) {
				$this->assign('jumpUrl', '__APP__/Admin/type');
				$this->success('删除成功！');
			}else{
				$this->error('删除出错！');
			}
		}else{
			$this->error('删除项不存在！');
		}
	}

    // 更新数据
	public function update() {

        $upload=$_FILES["upfile"];

        if($upload[size]=='0')
       {


		$type	=	M("article");

        $data['content']=stripslashes($_POST['content']);
           $data['title']=$_POST['title'];
           $data['tid']=$_POST['tid'];
           $data['key']=$_POST['key'];
           $data['desc']=$_POST['desc'];
           $data['fan']=$_POST['fan'];
           $data['creattime']=time();
           
           $taglist = $_POST['taglist'];
           $taglists = explode(",",$taglist);
           
		if($type->where('id='.$_POST['id'])->save($data))
        {
                            $dd=M("tag_article");
                            
                            $dd->where(" a_id = ".$_POST['id'])->delete();
                            
                            
                            foreach($taglists as $value){
                            
                            
                            if($value != ''){
                       
                                  
                                  
                                  $dc=M("tag");
                                  $one = $dc->where(" name = '".$value."' ")->find();
                                  
                                  $data1['a_id'] = $_POST['id'];
                                  $data1['t_id'] = $one['id']; 
                                  
                                  $dd->add($data1);
                                  
                              }    
                        
                        
                       }
                            
                            
                            
            
                            import ( '@.ORG.Gimage' );
                            preg_match_all("/<img.+src=\"(.+)\"/i",$data['content'],$match1);
                            $img = new Gimage();
                            if(count($match1[1])!=0){
                            foreach($match1[1] as $vl){

                                $ms=strrpos($vl,"/");
                                $jiehou=substr($vl,$ms+1);
                                $img->wm_image_name = "./ns/Public/Images/water_mark.gif";
                                $img->save_file="./ns/Public/Js/editor/attached/".$jiehou;
                                $img->create("./ns/Public/Js/editor/attached/".$jiehou);

                            }
                            }

                $this->assign('jumpUrl', '__APP__/Admin/index');
				$this->success('数据更新成功！');

		}
        else{
			$this->error($Form->getError());
		}
       }
       else
       {
           $type= M("article");
           $vs= $type->getById($_POST['id']);

           $filedir='./ns/Public/Uploads/'.$vs['pic'];
           @unlink($filedir);



           $filename=$upload[name];
           $k=strrpos($filename,".");
           $ext=substr($filename,$k+1);
          // echo $ext;exit;
           $newfilename=time().".".$ext;
           move_uploaded_file($upload[tmp_name],"./ns/Public/Uploads/".$newfilename);




           $data['content']=stripslashes($_POST['content']);
           $data['title']=$_POST['title'];
           $data['tid']=$_POST['tid'];
           $data['pic']=$newfilename;
           $data['key']=$_POST['key'];
           $data['desc']=$_POST['desc'];
           $data['creattime']=time();
           
           $taglist = $_POST['taglist'];
           $taglists = explode(",",$taglist);

           if($type->where('id='.$_POST['id'])->save($data))
           {
               
               
               
               
               $dd=M("tag_article");
                            
                            $dd->where(" a_id = ".$_POST['id'])->delete();
                            
                            
                            foreach($taglists as $value){
                            
                            
                            if($value != ''){
                       
                                  
                                  
                                  $dc=M("tag");
                                  $one = $dc->where(" name = '".$value."' ")->find();
                                  
                                  $data1['a_id'] = $_POST['id'];
                                  $data1['t_id'] = $one['id']; 
                                  
                                  $dd->add($data1);
                                  
                              }    
                        
                        
                       }
               
               
               
               
               

               import ( '@.ORG.CreatMiniature' );
            $cm=new CreatMiniature();
            $cm->SetVar("./ns/Public/Uploads/".$newfilename,"file");
            $cm->BackFill("./ns/Public/Uploads/".$newfilename,122,88);


            import ( '@.ORG.Gimage' );
                            preg_match_all("/<img.+src=\"(.+)\"/i",$data['content'],$match1);
                            $img = new Gimage();

                            if(count($match1[1])!=0){

                            foreach($match1[1] as $vl){

                                $ms=strrpos($vl,"/");
                                $jiehou=substr($vl,$ms+1);
                                $img->wm_image_name = "./ns/Public/Images/water_mark.gif";
                                $img->save_file="./ns/Public/Js/editor/attached/".$jiehou;
                                $img->create("./ns/Public/Js/editor/attached/".$jiehou);

                            }
                            }


               $this->assign('jumpUrl', '__APP__/Admin/index');
				$this->success('数据更新成功！');

           }
           else
           {
               $this->error("没有更新任何数据!");

           }

       }




	}
     // 更新分类
	public function updatetype() {
		$Form	=	D("type");
		if($vo = $Form->create()) {
			$list=$Form->save();
			if($list!==false){
                $this->assign('jumpUrl', '__APP__/Admin/type');
				$this->success('数据更新成功！');
			}else{
				$this->error("没有更新任何数据!");
			}
		}else{
			$this->error($Form->getError());
		}
	}
    
    public function chatag() {
        $name = $_POST['tags'];
        //echo $name;
        
		$dao	=	M("tag");
        $one = $dao->where(" name = '".$name."' ")->find();
        if($one || $name == ''){
           
            echo 'repeat';exit;
           
        }
        
        $data['name'] = $name;
        if($dao->add($data)){
        echo 'ok';
        
    }else{ echo 'failed';}
		
	}


    //修改页面
    public function edit() {


         if(!empty($_GET['id']))
        {

             $Model	=	new Model();
        $members_info=$Model->query("select * from ns_type order by path ");
foreach($members_info as $key=>$value)
    {

$dt="";


    if(substr_count($value['path'],',')>2)
   {

      for($i=0;$i<(substr_count($value['path'],',')-2);$i++)
        {
            $dt=$dt.'--';

        }

    }
    else
    {
     $dt="";

    }


          $members_info1[]=array

  (
      "id"=>$value["id"],
      "name"=>$dt."".$value["name"],
      "path"=>$value["path"]

  );



    		}





			$Form	=	M("article");
			$vo	=	$Form->getById($_GET['id']);
			if($vo) {
                $this->assign('list',$members_info1);
				$this->assign('vo',$vo);
                $this->assign('ss',$vo[tid]);
                
                
                
              $dao=M('tag');
        $taglist=$dao->order("id desc")->findAll();  
                $tgg="";
        foreach ($taglist as $value){
            
            $tgg.="<a class='addtag' mark='".$value['name']."' href='javascript:void(0)' >".$value['name']."</a>&nbsp;";
    
       }
        
        $this->assign("tgg",$tgg);
        
        
        $dao=M("tag_article");
        $tlist = $dao->where('a_id='.$vo['id'])->findAll();
        $ttlist = array();
        foreach($tlist as $value){
       
              $ttlist[]=$value['t_id'];     
        
        
       }
        $ttlists = implode(",",$ttlist);
        //dump($ttlists);
        
        $dao=M("tag");
        
        $tnlist = $dao->where(" id in (".$ttlists.") ")->findAll();
        
        $ttnlist = array();
        foreach($tnlist as $value){
       
              $ttnlist[]=$value['name'];     
        
        
       }
       
       $ttnlists = implode(",",$ttnlist);
        
       // dump($tnlist);
        
                
                $this->assign("ttn",$ttnlists.",");
                
				$this->display('edit');
			}else{
				exit('编辑项不存在123！');
			}
		}
        else{
			exit('编辑项不存在！');
		}
    }
    //修改分类名称
    public function edittype() {

         if(!empty($_GET['id'])) {
			$Form	=	M("type");
			$vo	=	$Form->getById($_GET['id']);
			if($vo) {
				$this->assign('vo',$vo);
				$this->display('edittype');
			}else{
				exit('编辑项不存在123！');
			}
		}
        else{
			exit('编辑项不存在！');
		}
    }

   //分类列表
    public function type()

    {/*
        import ( '@.ORG.Page_admin' );
        $smarty_page=new Page_admin();

        $sql="SELECT * FROM ns_type order by id desc";
        $page_info['num']=$smarty_page->Getallnum($sql);

        $page_info['page']=$smarty_page->Getallpage(20);
$page_info['link']=$smarty_page->Getpage(1,"__APP__/Admin/type/");
$page_info['start']=$smarty_page->Getstart(0);
$page_info['end']=$smarty_page->Getend(20);
$page_info['opt']=$smarty_page->options();




        $Model	=	new Model();
        $members_info=$Model->query("select * from ns_type order by path limit ".$page_info['start'].",".$page_info['end']."");
foreach($members_info as $key=>$value)
    {

$dt="";


    if(substr_count($value['path'],',')>2)
   {

      for($i=0;$i<(substr_count($value['path'],',')-2);$i++)
        {
            $dt=$dt.'--';

        }

    }
    else
    {
     $dt="";

    }


          $members_info1[]=array

  (
      "id"=>$value["id"],
      "name"=>$dt."".$value["name"],
      "path"=>$value["path"]

  );



    		}*/




        $dao=M("type");
        $list=$dao->findAll();
        $this->assign('list',$list);
		$this->display('type');





    }
    //添加顶级分类
    public  function inserttoptype()
    {

        $Form	=	M("type");
		if($Form->create()) {
			if(false !==$Form->add())
            {
                $Model	=	new Model();
                $vo	=	$Form->getById(mysql_insert_id());
                $data['path']=','.$vo['id'].",";
                $type= M("type");
                $type->where('id='.$vo['id'])->save($data);


			    $this->assign('jumpUrl', '__APP__/Admin/type');
    			$this->success('数据添加成功！');
            }
            else
            {
                $this->error('数据写入错误');
            }
		}else{
			header("Content-Type:text/html; charset=utf-8");
			exit($Form->getError().' [ <A HREF="javascript:history.back()">返 回</A> ]');
		}



    }

    //添加子分类页面
    public  function addtype()
    {

        if(!empty($_GET['id'])) {
			$Form	=	M("type");
			$vo	=	$Form->getById($_GET['id']);
			if($vo) {
				$this->assign('vo',$vo);
				$this->display('addtype');
			}else{
				exit('编辑项不存在！');
			}
		}
        else{
			exit('编辑项不存在！');
		}



    }
    //添加子分类
    public  function inserttype()
    {
        $fatherid=$_POST['id'];
        $name=$_POST['name'];
        $Form	=	M("type");
		$vo	=	$Form->getById($fatherid);

        $data['name']=$name;
        $data['path']=$vo['path'];

        if($Form->data($data)->add())
        {

        $vs= $Form->getById(mysql_insert_id());

        $data['path']=$vs['path']."".$vs['id'].",";
        $type= M("type");
        $type->where('id='.$vs['id'])->save($data);



			    $this->assign('jumpUrl', '__APP__/Admin/type');
    			$this->success('数据添加成功！');
       }
       else
        {
           exit('编辑项不存在！');

       }






    }
    //登出
     public function logout() {

        session_destroy();
        $this->redirect('Index/admincp');
    }

    //清除缓存
    public function clearcache()
    {
        import('ORG.Io.Dir');
		$dir=new Dir();
		//清空后台缓存



		$dir->delDir('./ns/Runtime','true');

        $this->success('成功！');

    }

//清除静态缓存
    public function clearhtml()
    {
       import('ORG.Io.Dir');
		$dir=new Dir();
		//清空后台缓存


		$dir->delDir('./ns/Html','true');

        $this->success('成功！');

    }

    public function createrss() {

       // echo "123";
        include('./ns/Lib/Action/rss74.php');
        $rss = new UniversalFeedCreator();
        $rss->title = "小号手之歌";
        $rss->link = "http://www.xiaohao.net";
        $rss->description = "小号手之家提供小号演奏,乐器小号,小号配件,小号培训,小号号嘴,小号乐谱，高音小号,小号商城,雅马哈小号,巴哈小号,星海小号,是专业的小号类门户网站";
        $dao=M("article");
        $softList = $dao->order("id desc")->limit("3000")->findAll();
        foreach ($softList as $rs) {
        $item = new FeedItem();
        $item->title = $rs['title'];
        $item->link = "http://www.xiaohao.net/c/".$rs['id'].".html";
        $item->description = $rs['desc'];
        $rss->addItem($item);
        }
        $rss->saveFeed("RSS2.0", "rss.xml");
        $this->success('成功！');

    }

}
?>
